package com.noble.meether.chat.ui.agora.act.comment;

import android.content.Context;
import android.text.TextUtils;
import android.widget.EditText;

import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.config.UrlParam;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.library.log.PToast;
import com.noble.library.view.base.BasePanel;
import com.noble.meether.R;
import com.noble.meether.chat.ui.agora.act.bean.LabelList;
import com.noble.meether.chat.ui.agora.view.RatingBarView;
import com.noble.meether.chat.ui.agora.view.ViewAutoWrap;

import java.util.List;

/**
 * 视频结束后评价
 * Created by Su on 2017/7/19.
 */
public class RtcCommentFootPanel extends BasePanel implements RequestComplete {
    private RatingBarView barView;      // 星级评价
    private ViewAutoWrap autoWrap;      // 流布局
    private EditText tv_edit;


    public RtcCommentFootPanel(Context context) {
        super(context);
        setContentView(R.layout.f1_chat_video_comment_foot_panel);
        initView();
    }

    private void initView() {
        autoWrap = (ViewAutoWrap) findViewById(R.id.view_comment_content);
        barView = (RatingBarView) findViewById(R.id.custom_ratingbar);
        tv_edit = (EditText) findViewById(R.id.tv_edit);
        ModuleMgr.getCenterMgr().inputFilterSpace(tv_edit, 120);
        ModuleMgr.getRtcChatMgr().reqGetFeedbackInfo(this);
    }

    public void onSubmit() {
        if (barView.getStarCount() == 0) {
            PToast.showShort("请选择评价星级");
            return;
        }
        String edit = tv_edit.getText().toString();
        List<String> submit = autoWrap.getSelectedString();
        if (!TextUtils.isEmpty(edit)) submit.add(edit);
        ModuleMgr.getRtcChatMgr().reqAddFeedback(barView.getStarCount(), submit, this);
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.getUrlParam() == UrlParam.reqGetFeedbackInfo) {
            if (!response.isOk()) return;
            LabelList labelList = (LabelList) response.getBaseData();
            if (labelList == null || labelList.getLabelList().size() <= 0) return;
            autoWrap.removeAllViews();

            // 根据星级展示不同标签
            barView.setOnRatingListener((bindObject, RatingScore) -> {
                if (RatingScore < 4) {
                    autoWrap.removeAllViews();
                    autoWrap.init(labelList.getCriticizeLabel(), getContext(), false);
                } else {
                    autoWrap.removeAllViews();
                    autoWrap.init(labelList.getPraiseLabel(), getContext(), false);
                }
            });
            return;
        }

        if (response.getUrlParam() == UrlParam.reqAddFeedback) {
            if (!response.isOk()) {
                PToast.showShort(getContext().getString(R.string.chat_video_submit_error));
                return;
            }
            PToast.showShort(getContext().getString(R.string.chat_video_submit_suc));
            finishActivity();
        }
    }

    private void finishActivity() {
        getActivity().finish();
    }
}
